//
//  WDEditImageCell.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/2/17.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDEditImageCell.h"
#import "WDUploadData.h"
#import "WDEdgeLabel.h"
#import "SkyWaitingView.h"
#import <AudioToolbox/AudioToolbox.h>

@interface WDEditImageCell ()
@property (weak, nonatomic) IBOutlet UIImageView *loanImage;
@property (weak, nonatomic) IBOutlet UIView *coverView;
@property (nonatomic, strong) UILabel *finishLabel;
/** 删除按钮 */
@property (weak, nonatomic) IBOutlet UIButton *deleBtn;
@property (nonatomic, strong) SkyLabelWaitingView *waitView;

@end

@implementation WDEditImageCell

- (void)setUploadData:(WDUploadData *)uploadData
{
    _uploadData = uploadData;
    
    uploadData.status = self.uploadStatus;
    
    if (!uploadData.status) {
        
        //转圈蒙版
        self.finishLabel.hidden = YES;
        
        self.waitView.hidden = NO;
        
         self.loanImage.image = [UIImage imageWithContentsOfFile:[WDCachePath stringByAppendingPathComponent:uploadData.imagePath]];
        
        self.loanImage.image = [UIImage scaleToSize:self.loanImage.image size:CGSizeMake(117, 87)];
        //WDLog(@"%f,%f", self.loanImage.image.size.width, self.loanImage.image.size.height);
    } else {
        
        //已上传蒙版
        self.coverView.backgroundColor = ColorWithAlpha(0x5f5f5f, 0.7);
        
        self.finishLabel.hidden = NO;
        
        self.waitView.hidden = YES;
        
        if (uploadData.imagePath) {
            self.loanImage.image = [UIImage imageWithContentsOfFile:[WDCachePath stringByAppendingPathComponent:uploadData.imagePath]];
            
            self.loanImage.image = [UIImage scaleToSize:self.loanImage.image size:CGSizeMake(117, 87)];
            
        } else {
            [self.loanImage sd_setImageWithURL:[NSURL URLWithString:uploadData.thumbUrl]];
            
            self.loanImage.image = [UIImage scaleToSize:self.loanImage.image size:CGSizeMake(117, 87)];
        }
        
    }
}

- (void)laodWaitView
{
    SkyLabelWaitingView *lv = [SkyLabelWaitingView new];
    lv.ringColor = [UIColor grayColor];
    lv.r = 20;
    lv.ringWidth = 5.0f;
    lv.rate = 0.5f;
    lv.label.font = [UIFont systemFontOfSize:12.f];
    lv.frame = CGRectMake(38.5, 13.5, 0, 0);
    [lv sizeToFit];
    [self.coverView addSubview:lv];
    self.waitView = lv;
    [lv start];
}

- (IBAction)clickDeleBtn:(id)sender {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.uploadData.objectId forKey:@"seleStr"];
    
    _deleBtn.selected = !_deleBtn.selected;
    if (_deleBtn.selected) {
        [dict setObject:@1 forKey:@"selectStatus"];
    } else {
        [dict setObject:@0 forKey:@"selectStatus"];
    }
    
    //选中\未选中图片的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"isChoosed" object:@"编辑图片发通知" userInfo:dict];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //添加长按手势
    UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGr.minimumPressDuration = 1.0;
    [self addGestureRecognizer:longPressGr];
    
    self.loanImage.contentMode = UIViewContentModeScaleAspectFill;
    self.loanImage.clipsToBounds = YES;
    
    //图片加边框
    self.loanImage.layer.borderWidth = 0.5f;
    self.loanImage.layer.borderColor = ColorForRGB(0xa0a0a0).CGColor;
    
    WDEdgeLabel *label = [[WDEdgeLabel alloc] init];
    label.edgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);//内边距
    label.font = [UIFont systemFontOfSize:12];
    label.text = @"已上传";
    [label sizeToFit];
    label.layer.cornerRadius = 10;
    label.clipsToBounds = YES;
    label.backgroundColor = ColorForRGB(0x4f4f4f);
    label.textColor = [UIColor colorWithWhite:1 alpha:1];
    label.textAlignment = NSTextAlignmentCenter;
    [self.coverView addSubview:label];
    self.finishLabel = label;
    label.hidden = YES;
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.coverView);
        make.centerY.equalTo(self.coverView);
    }];
    
    [self laodWaitView];
}

//长按手势
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
    //遍历数据库，查看是否有未上传的
    WDSQLiteManager *manager = [WDSQLiteManager manager];
    
    NSArray *waitArr = [manager selectWithTable:NSStringFromClass([WDUploadData class]) whereCondition:@{@"status":@(UploadStatusWaiting)}];
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        
        if (!self.addImage) {
            
            if (!self.isShowBtn && waitArr.count == 0) {//编辑模式不响应长按手势
                
                AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
                
                self.deleBtn.hidden = NO;
                self.deleBtn.selected = YES;
                
                NSDictionary *dict = @{@"editMode":@1,@"seleStr":self.uploadData.objectId};
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"isEditMode" object:@"长按手势发送通知" userInfo:dict];
                
                NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
                [dict1 setObject:self.uploadData.objectId forKey:@"seleStr"];
                
                if (_deleBtn.selected) {
                    [dict1 setObject:@1 forKey:@"selectStatus"];
                } else {
                    [dict1 setObject:@0 forKey:@"selectStatus"];
                }
                
                //选中\未选中图片的通知
                [[NSNotificationCenter defaultCenter] postNotificationName:@"isChoosed" object:@"编辑图片发通知" userInfo:dict1];
            }
            
        }
    }
}

- (void)setAddImage:(BOOL)addImage
{
    _addImage = addImage;
    
    if (addImage) {
        
            self.loanImage.contentMode = UIViewContentModeCenter;
        
        self.loanImage.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.4];
        self.loanImage.layer.borderWidth = 0.5;
        self.loanImage.layer.borderColor = [UIColor colorWithWhite:0.3 alpha:0.6].CGColor;
        
        self.finishLabel.hidden = YES;
        self.coverView.hidden = YES;
        
        self.loanImage.image = [UIImage imageNamed:@"icon-tianjiazhaopian"];
    }else{
        self.loanImage.backgroundColor = [UIColor clearColor];
        self.coverView.hidden = NO;
    }
}

- (void)setIsShowBtn:(BOOL)isShowBtn
{
    _isShowBtn = isShowBtn;
    
    if (isShowBtn) {//是编辑模式
        
        self.deleBtn.hidden = NO;
    } else {
        
        self.deleBtn.hidden = YES;
        
        //退出编辑模式，恢复默认，取消选中
        self.deleBtn.selected = NO;

    }
    
}

- (void)setIsSelectedBtn:(BOOL)isSelectedBtn
{
    _isSelectedBtn = isSelectedBtn;
    
    self.deleBtn.selected = !self.deleBtn.selected;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.uploadData.objectId forKey:@"seleStr"];
    
    if (_deleBtn.selected) {
        [dict setObject:@1 forKey:@"selectStatus"];
    } else {
        [dict setObject:@0 forKey:@"selectStatus"];
    }
    
    //选中\未选中图片的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"isChoosed" object:@"编辑图片发通知" userInfo:dict];
}

- (void)setUploadStatus:(NSInteger)uploadStatus
{
    _uploadStatus = uploadStatus;
}

@end
